package com.prawn.ift_collequipment.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.prawn.ift_collequipment.dao.EquipmentChannelDao;
import com.prawn.ift_collequipment.pojo.EquipmentChannel;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class EquipmentChannelService {

	@Autowired
	private EquipmentChannelDao equipmentChannelDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<EquipmentChannel> findAll() {
		return equipmentChannelDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<EquipmentChannel> findSearch(Map whereMap, int page, int size) {
		Specification<EquipmentChannel> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return equipmentChannelDao.findAll(specification, pageRequest);
	}

	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<EquipmentChannel> findSearch(Map whereMap) {
		Specification<EquipmentChannel> specification = createSpecification(whereMap);
		return equipmentChannelDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	@Cacheable(value = "equipmentChannel",key = "#id")
	public EquipmentChannel findById(String id) {
		return equipmentChannelDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param equipmentChannel
	 */
	public void add(EquipmentChannel equipmentChannel) {
		equipmentChannel.setId( idWorker.nextId()+"" );
		equipmentChannelDao.save(equipmentChannel);
	}

	/**
	 * 修改
	 * @param equipmentChannel
	 */
	@CacheEvict(value = "equipmentChannel",key = "#equipmentChannel.id")
	public void update(EquipmentChannel equipmentChannel) {
		equipmentChannelDao.save(equipmentChannel);
	}

	/**
	 * 删除
	 * @param id
	 */
	@CacheEvict(value = "equipmentChannel",key = "#id")
	public void deleteById(String id) {
		equipmentChannelDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<EquipmentChannel> createSpecification(Map searchMap) {

		return new Specification<EquipmentChannel>() {

			@Override
			public Predicate toPredicate(Root<EquipmentChannel> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 通道编号
                if (searchMap.get("channelId")!=null && !"".equals(searchMap.get("channelId"))) {
                	predicateList.add(cb.like(root.get("channelId").as(String.class), "%"+(String)searchMap.get("channelId")+"%"));
                }
                // 通道号
                if (searchMap.get("channelNO")!=null && !"".equals(searchMap.get("channelNO"))) {
                	predicateList.add(cb.like(root.get("channelNO").as(String.class), "%"+(String)searchMap.get("channelNO")+"%"));
                }
                // 通道名称
                if (searchMap.get("channelName")!=null && !"".equals(searchMap.get("channelName"))) {
                	predicateList.add(cb.like(root.get("channelName").as(String.class), "%"+(String)searchMap.get("channelName")+"%"));
                }
                // 设备编号
                if (searchMap.get("equipmentId")!=null && !"".equals(searchMap.get("equipmentId"))) {
                	predicateList.add(cb.like(root.get("equipmentId").as(String.class), "%"+(String)searchMap.get("equipmentId")+"%"));
                }
                // 
                if (searchMap.get("equipmentName")!=null && !"".equals(searchMap.get("equipmentName"))) {
                	predicateList.add(cb.like(root.get("equipmentName").as(String.class), "%"+(String)searchMap.get("equipmentName")+"%"));
                }
                // 
                if (searchMap.get("sensorId")!=null && !"".equals(searchMap.get("sensorId"))) {
                	predicateList.add(cb.like(root.get("sensorId").as(String.class), "%"+(String)searchMap.get("sensorId")+"%"));
                }
                // 采集通道类型
                if (searchMap.get("collectChannelApplicateTypeId")!=null && !"".equals(searchMap.get("collectChannelApplicateTypeId"))) {
                	predicateList.add(cb.like(root.get("collectChannelApplicateTypeId").as(String.class), "%"+(String)searchMap.get("collectChannelApplicateTypeId")+"%"));
                }
                // 处理数据方式
                if (searchMap.get("channelDataTreatmentMode")!=null && !"".equals(searchMap.get("channelDataTreatmentMode"))) {
                	predicateList.add(cb.like(root.get("channelDataTreatmentMode").as(String.class), "%"+(String)searchMap.get("channelDataTreatmentMode")+"%"));
                }
                // 存储周期
                if (searchMap.get("saveTime")!=null && !"".equals(searchMap.get("saveTime"))) {
                	predicateList.add(cb.like(root.get("saveTime").as(String.class), "%"+(String)searchMap.get("saveTime")+"%"));
                }
                // 小数数位
                if (searchMap.get("decimalDigit")!=null && !"".equals(searchMap.get("decimalDigit"))) {
                	predicateList.add(cb.like(root.get("decimalDigit").as(String.class), "%"+(String)searchMap.get("decimalDigit")+"%"));
                }
                // 原始数据单位
                if (searchMap.get("originalDataUnit")!=null && !"".equals(searchMap.get("originalDataUnit"))) {
                	predicateList.add(cb.like(root.get("originalDataUnit").as(String.class), "%"+(String)searchMap.get("originalDataUnit")+"%"));
                }
                // 校正单位
                if (searchMap.get("correctedUnit")!=null && !"".equals(searchMap.get("correctedUnit"))) {
                	predicateList.add(cb.like(root.get("correctedUnit").as(String.class), "%"+(String)searchMap.get("correctedUnit")+"%"));
                }
                // 量程上限
                if (searchMap.get("rangeCeiling")!=null && !"".equals(searchMap.get("rangeCeiling"))) {
                	predicateList.add(cb.like(root.get("rangeCeiling").as(String.class), "%"+(String)searchMap.get("rangeCeiling")+"%"));
                }
                // 量程下限
                if (searchMap.get("rangeLowerLimit")!=null && !"".equals(searchMap.get("rangeLowerLimit"))) {
                	predicateList.add(cb.like(root.get("rangeLowerLimit").as(String.class), "%"+(String)searchMap.get("rangeLowerLimit")+"%"));
                }
                // 变化率上限
                if (searchMap.get("upperLimitOfChangeRate")!=null && !"".equals(searchMap.get("upperLimitOfChangeRate"))) {
                	predicateList.add(cb.like(root.get("upperLimitOfChangeRate").as(String.class), "%"+(String)searchMap.get("upperLimitOfChangeRate")+"%"));
                }
                // 校准周期
                if (searchMap.get("calibrationCycle")!=null && !"".equals(searchMap.get("calibrationCycle"))) {
                	predicateList.add(cb.like(root.get("calibrationCycle").as(String.class), "%"+(String)searchMap.get("calibrationCycle")+"%"));
                }
                // 校准公式
                if (searchMap.get("calibrationFormula")!=null && !"".equals(searchMap.get("calibrationFormula"))) {
                	predicateList.add(cb.like(root.get("calibrationFormula").as(String.class), "%"+(String)searchMap.get("calibrationFormula")+"%"));
                }
                // 位置编号
                if (searchMap.get("locationNumber")!=null && !"".equals(searchMap.get("locationNumber"))) {
                	predicateList.add(cb.like(root.get("locationNumber").as(String.class), "%"+(String)searchMap.get("locationNumber")+"%"));
                }
                // 通道设备类型
                if (searchMap.get("channelDeviceType")!=null && !"".equals(searchMap.get("channelDeviceType"))) {
                	predicateList.add(cb.like(root.get("channelDeviceType").as(String.class), "%"+(String)searchMap.get("channelDeviceType")+"%"));
                }
                // 是否对外服务
                if (searchMap.get("serviceAvailable")!=null && !"".equals(searchMap.get("serviceAvailable"))) {
                	predicateList.add(cb.like(root.get("serviceAvailable").as(String.class), "%"+(String)searchMap.get("serviceAvailable")+"%"));
                }
                // 通道深度
                if (searchMap.get("channelDepth")!=null && !"".equals(searchMap.get("channelDepth"))) {
                	predicateList.add(cb.like(root.get("channelDepth").as(String.class), "%"+(String)searchMap.get("channelDepth")+"%"));
                }
                // 数据类型
                if (searchMap.get("dataType")!=null && !"".equals(searchMap.get("dataType"))) {
                	predicateList.add(cb.like(root.get("dataType").as(String.class), "%"+(String)searchMap.get("dataType")+"%"));
                }
                // 
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                	predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 备注
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                	predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
